package com.example.customview.view

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import com.example.customview.R
import com.sample.base.utils.dp

class MaterialEditTextView(context: Context?, attrs: AttributeSet?) :
    AppCompatEditText(context, attrs) {
    @SuppressLint("NewApi")
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        textSize = FLOAT_TEXT_SIZE.toFloat()
        color = resources.getColor(R.color.colorGrey, resources.newTheme())
    }

    companion object {
        private val FLOAT_TEXT_SIZE = 12.dp
        private val PADDING_MARGIN_BETWEEN_TOP_AND_BOTTOM = 6.dp
        private val HORIZONTAL_OFFSET = 4.dp
        private val VERTICAL_OFFSET = 20.dp

        // 文字上下偏移做动画的距离（从底部上去、从上面下来）合适就好
        private val ANIMATOR_VERTICAL_OFFSET = 12.dp
    }

    private val textBounds = Rect()
    private var floatLabelShown = false


    var floatLabelFraction = 0f
        set(value) {
            field = value
            invalidate()
        }

    var useFloatingLabel = false
        set(value) {
            if (field != value) {
                field = value
                if (field) {
                    setPadding(
                        paddingLeft,
                        paddingTop + (FLOAT_TEXT_SIZE + PADDING_MARGIN_BETWEEN_TOP_AND_BOTTOM),
                        paddingRight,
                        paddingBottom
                    )
                } else {
                    setPadding(
                        paddingLeft,
                        paddingTop - FLOAT_TEXT_SIZE - PADDING_MARGIN_BETWEEN_TOP_AND_BOTTOM,
                        paddingRight,
                        paddingBottom
                    )
                }
            }
        }
    private var animator = ObjectAnimator.ofFloat(this, "floatLabelFraction", 0f, 1f)

    override fun onTextChanged(
        text: CharSequence?,
        start: Int,
        lengthBefore: Int,
        lengthAfter: Int
    ) {
        if (useFloatingLabel) {
            if (floatLabelShown && text.isNullOrEmpty()) {
                floatLabelShown = false
                animator.reverse()
            }
            // 从无到有
            else if (!floatLabelShown && !text.isNullOrEmpty()) {
                floatLabelShown = true
                animator.start()
            }
        }
        // 从有到无

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (useFloatingLabel) {
            val hintText = hint.toString()
            paint.getTextBounds(hintText, 0, hintText.length, textBounds)
            paint.alpha = (floatLabelFraction * 0xff).toInt()
            val cVerticalOffset =
                VERTICAL_OFFSET.toFloat() + ANIMATOR_VERTICAL_OFFSET * (1 - floatLabelFraction)

            canvas.drawText(
                hint.toString(), textBounds.left.toFloat() + HORIZONTAL_OFFSET.toFloat(),
                cVerticalOffset, paint
            )

        }


    }
}